function  MyPromise(executor) {
  // 添加属性
  this.PromiseState = 'pending'
  this.PromiseResult = null
  // 声明一个属性保存回调函数
  this.callbacks = []
  // 保存实例对象的this值
  const self = this
  function resolve(data) {
    if (self.PromiseState !== 'pending') return
    // this指向window
    // 1. 修改对象的状态
    // this.PromiseState = 'fulfilled'
    self.PromiseState = 'fulfilled'
    // 2. 设置对象的结果值
    // this.PromiseResult = data
    self.PromiseResult = data
    // 调用成功的回调函数
    // if (self.callback.onResovled) {
    //   self.callback.onResovled(data)
    // }
    // 将then变为异步的
    setTimeout(() => {
      self.callbacks.forEach(item => {
        item.onResovled(data)
      })
    })
  }
  function reject(data) {
    if (self.PromiseState !== 'pending') return
    // this指向window
    // this.PromiseState = 'rejected'
    self.PromiseState = 'rejected'
    // this.PromiseResult = data
    self.PromiseResult = data
    // if (self.callback.onRejected) {
    //   self.callback.onRejected(data)
    // }
    setTimeout(() => {
      self.callbacks.forEach(item => {
        item.onRejected(data)
      })
    })
  }
  // 执行器函数是同步调用的
  try {
    executor(resolve, reject)
  } catch (e) {
    // 修改promise对象状态为失败
    reject(e)
  }
}

MyPromise.prototype.then = function(onResovled, onRejected) {
  const self = this
  if (typeof onRejected !== 'function') {
    onRejected = reason => {
      throw reason
    }
  }
  if (typeof onResovled !== 'function') {
    onResovled = value => value
  }
  return new MyPromise((resolve, reject) => {
    // 封装函数
    function callback(type) {
      try {
        let result = type(self.PromiseResult)
        if (result instanceof MyPromise) {
          result.then(v => {
            resolve(v)
          }, r => {
            reject(r)
          })
        } else {
          // 结果的对象状态为成功
          resolve(result)
        }
      } catch(e) {
        reject(e)
      }
    }
    // 调用回调函数
    //也可以先改状态再指定回调（同步任务）
    if (this.PromiseState === 'fulfilled') {
      // 将then变为异步的
      setTimeout(() => callback(onResovled))
    }
    if (this.PromiseState === 'rejected') {
      // 将then变为异步的
      setTimeout(() => callback(onRejected))
    }
    //先指定回调再改变状态（异步任务）
    if (this.PromiseState === 'pending') {
      // 保存回调函数，状态不确定
      // 这样保存回调不对，只能保存最后一个，前面的会被覆盖
      // this.callback = {
      //   onResovled,
      //   onRejected
      // }
      this.callbacks.push({
        onResovled: function() {
          callback(onResovled)
        },
        onRejected: function() {
          callback(onRejected)
        }
      })
    }
  })
}

MyPromise.prototype.catch = function (onRejected) {
  return this.then(undefined, onRejected)
}

// 添加resolve
MyPromise.resolve = function(value) {
  return new MyPromise((resolve, reject) => {
    if (value instanceof MyPromise) {
      value.then(v => resolve(v), r => reject
      (r) )
    } else {
      resolve(value)
    }
  })
}

// 添加reject
MyPromise.reject = function(value) {
  return new MyPromise((resolve, reject) => {
    reject(value)
  })
}

//添加all
MyPromise.all = function(promises) {
  return new MyPromise((resolve, reject) => {
    let count = 0
    let arr = []
    promises.forEach((promise,index) => promise.then(
      v => {
        // 成功对象会调用
        count++
        arr[index] = v
        if (count === promises.length) {
          // 修改状态
          resolve(arr)
        }
      },
      r => {
        reject(r)
      }
    ))
  })
}

//添加race
MyPromise.race = function (promises) {
  return new MyPromise((resolve, reject) => {
    promises.forEach((promise, index) => {
      promise.then(v => {
        resolve(v)
      }, r => {
        reject(r)
      })
    })
  })
}